<template>
	<view>
		<view class="cu-form-group">
			<view class="title">姓名</view>
			<input placeholder="请输入你的名字" name="name" v-model="inputForm.name"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">学号</view>
			<input placeholder="请输入你的学号" name="studentId" v-model="inputForm.studentId"></input>
		</view>

		<view class="margin-top">
			<view v-for="(item,index) in list" :key="index">
				<view class="cu-form-group ">
					<view class="title">{{item.entryTime}}</view>
					<button form-type="submit" class="cu-btn block bg-gradual-blue margin-tb-sm lg" :data-id="index"
						@tap="signOut">签退</button>
				</view>
			</view>
		</view>
		<view v-if="reachable" class="padding-xl">
			<button form-type="submit" class="cu-btn block bg-gradual-blue margin-tb-sm lg"
				@click="signIn()">扫码签到</button>
		</view>
	</view>

</template>
<script>
	var graceChecker = require("@/common/graceChecker.js");
	export default {
		data() {
			return {
				loading: false,
				inputForm: {
					name: '', // 姓名
					studentId: '', // 学号
					classroom: '', // 教室
					entryTime: '', // 开始时间
					departureTime: '' // 离开时间
				},
				list: [], // 未签退信息表
				reachable: false // 可不可以扫码
			}
		},
		onShow() {
			this.getStorage()
		},
		methods: {
			// 查未签退
			findAllNotgoByStudentId(e) { 
				uni.showLoading()
				if (this.check(this.inputForm)) {
					// uni.showLoading()
					this.$http.get('/selfstudyinformation/selfStudyInformation/findAllNotgoByStudentId', this.inputForm)
						.then(({
							data
						}) => {
							this.list = data.list
							if (this.list.length == 0) {
								// 可以签到的flag
								this.reachable = true 
							} else {
								this.reachable = false
							}
							uni.showToast({
								title: data.msg,
								icon: "success"
							});
						}).catch((e) => {
							uni.showToast({
								title: data.msg,
								icon: "error"
							});
						})
				} else {
					uni.showToast({
						title: "信息不能为空",
						icon: "none"
					});
				}
			},
			setStorage(inpf) {
				wx.setStorage({
					key: "name",
					data: inpf.name
				})
				wx.setStorage({
					key: "studentId",
					data: inpf.studentId
				})
			},
			// 查缓存
			getStorage() {
				var __this = this
				wx.getStorage({
					key: 'name',
					success(res) {
						__this.inputForm.name = res.data
					},
					fail(res) {}
				})
				wx.getStorage({
					key: 'studentId',
					success(res) {
						__this.inputForm.studentId = res.data
						__this.findAllNotgoByStudentId()
					},
					fail(res) {
						__this.reachable = true
					}
				})
			},
			// 扫码签到
			signIn() { 
				uni.showLoading()
				var __this = this
				if (this.check(this.inputForm)) {
					wx.scanCode({
						// 成功之后
						success(res) { 
							__this.inputForm.classroom = res.result
							__this.saveInf() 
						}
					})
				} else {
					uni.showToast({
						title: "您有未填写信息",
						icon: "none"
					});
				}
			},
			// 签退
			signOut(e) { 
				uni.showLoading()
				var __this = this
				wx.scanCode({
					// 成功之后
					success(res) { 
						__this.saveOutTime(e)
					}
				})
			},
			 // 登出的请求
			saveOutTime(e) {
				var index = e.target.dataset.id
				this.list[index].departureTime = this.getCurrentTime()
				this.$http.get('/selfstudyinformation/selfStudyInformation/updateGoTime', this.list[index]).then(({
					data
				}) => {
					if (data && data.success) {
						this.findAllNotgoByStudentId()
					}
				}).catch((e) => {
				uni.showToast({
						title: data.msg,
						icon: "error"
					});
				})
			},
			 // 保存信息
			saveInf() {
				var __this = this
				this.inputForm.entryTime = this.getCurrentTime()
				if (this.check(this.inputForm)) {
					// uni.showLoading()
					this.$http.post('/selfstudyinformation/selfStudyInformation/save', this.inputForm).then(({
						data
					}) => {
						__this.setStorage(__this.inputForm)
						__this.findAllNotgoByStudentId()
					}).catch((e) => {
						uni.showToast({
							title: data.msg,
							icon: "error"
						});
					})
				} else {
					uni.showToast({
						title: "存在未填写信息",
						icon: "none"
					});
				}
			},
			check(tar) {
				// 定义表单规则
				var rule = [{
						name: "name",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "姓名不能为空"
					},
					{
						name: "studentId",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "学号不能为空"
					}
				];
				// 进行表单检查
				var formData = this.inputForm
				return graceChecker.check(formData, rule)
			},
			getCurrentTime() {
				// 获取当前时间并打印
				var _this = this;
				let yy = new Date().getFullYear();
				let mm = new Date().getMonth() + 1;
				let dd = new Date().getDate();
				let hh = new Date().getHours();
				let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
				let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
				return yy + '/' + mm + '/' + dd + ' ' + hh + ':' + mf + ':' + ss;
			}
		}
	}
</script>

<style>

</style>
